﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThreadTest
{
    public partial class Form1 : Form
    {
        Process process;
        public Form1()
        {
            Console.WriteLine("load:" + Thread.CurrentThread.ManagedThreadId);
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process[] processes = Process.GetProcesses();
            foreach (var item in processes)
            {
                Console.WriteLine(item.ProcessName);
            }
            Console.WriteLine("一共"+processes.Length+"个进程。");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Process.Start("notepad++");
            //Process.Start("calc");
            process = Process.Start("msedge","www.baidu.com");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            process.Kill();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Thread t1 = new Thread(new ThreadStart(
                () =>
                {
                    for (int i = 0; i < 1000000; i++)
                    {
                        Console.WriteLine(i);
                    }
                    Console.WriteLine("click:"+Thread.CurrentThread.ManagedThreadId);
                }
                ));
            t1.IsBackground = true;
            t1.Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Thread t2 = new Thread(M2);
            t2.Start();
        }

        private void M2()
        {
            for (int i = 0; i < 5; i++)
            {
                textBox1.Text = i.ToString();
                textBox1.Invoke(new Action<int>(num => textBox1.Text = num.ToString()),i);
                Thread.Sleep(500);
            }
        }
    }
}
